package com.example.personalizededucationsystem.controller;

import com.example.personalizededucationsystem.dto.dto.AssignmentSubmissionSummaryDto;
import com.example.personalizededucationsystem.dto.dto.GradeSubmissionRequestDto;
import com.example.personalizededucationsystem.dto.dto.StudentAnswerDetailDto;
import com.example.personalizededucationsystem.dto.dto.TeacherAssignmentDto;
import com.example.personalizededucationsystem.dto.request.AssignmentSubmissionRequest;
import com.example.personalizededucationsystem.dto.request.CreateAssignmentRequest;
import com.example.personalizededucationsystem.dto.response.AssignmentResponse;
import com.example.personalizededucationsystem.model.Assignment;
import com.example.personalizededucationsystem.model.User;
import com.example.personalizededucationsystem.service.AssignmentService;
import com.example.personalizededucationsystem.utils.IP_VALUE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin(origins = IP_VALUE.IP)
@RestController
@RequestMapping("/api/assignments")
public class AssignmentController {

    private final AssignmentService assignmentService;

    @Autowired
    public AssignmentController(AssignmentService assignmentService) {
        this.assignmentService = assignmentService;
    }

    @PostMapping
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<AssignmentResponse> createAssignment(@RequestBody CreateAssignmentRequest request, @AuthenticationPrincipal User currentUser) {
        Assignment assignmentEntity = assignmentService.createAssignment(request, currentUser);
        AssignmentResponse responseDto = AssignmentResponse.fromEntity(assignmentEntity);
        return new ResponseEntity<>(responseDto, HttpStatus.CREATED);
    }

    @GetMapping("/class/{classId}")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<List<AssignmentResponse>> getAssignmentsForClass(@PathVariable Long classId) {
        return ResponseEntity.ok(assignmentService.getAssignmentByClass(classId));
    }

    @PostMapping("/{assignmentId}/submit")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<Void> submitAssignment(@PathVariable Long assignmentId, @RequestBody AssignmentSubmissionRequest request, @AuthenticationPrincipal User currentUser) {
        assignmentService.submitAssignment(assignmentId, request, currentUser);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/class/{classId}/submission")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<List<TeacherAssignmentDto>> getAssignmentsWithSubmissionsByClass(@PathVariable Long classId, @AuthenticationPrincipal User currentUser) {
        return ResponseEntity.ok(assignmentService.getAssignmentsByClass(classId, currentUser));
    }


    @GetMapping("/{assignmentId}/submissions")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<List<AssignmentSubmissionSummaryDto>> getSubmissionsForAssignment(@PathVariable Long assignmentId, @AuthenticationPrincipal User currentUser) {
        List<AssignmentSubmissionSummaryDto> submissions = assignmentService.getSubmissionsForAssignment(assignmentId, currentUser)
                .stream()
                .map(AssignmentSubmissionSummaryDto::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(submissions);
    }

    @PostMapping("/submissions/{studentAssignmentId}/grade")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<Void> gradeSubmission(@PathVariable Long studentAssignmentId, @RequestBody GradeSubmissionRequestDto request, @AuthenticationPrincipal User currentUser) {
        assignmentService.gradeSubmission(studentAssignmentId, request, currentUser);
        return ResponseEntity.ok().build();

    }

    @GetMapping("/{assignmentId}/submissions/{studentId}")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<List<StudentAnswerDetailDto>> getStudentSubmissionDetail(@PathVariable Long assignmentId, @PathVariable Long studentId, @AuthenticationPrincipal User currentUser) {
        return ResponseEntity.ok(assignmentService.getSubmissionDetail(assignmentId, studentId, currentUser));
    }

    @GetMapping("/{assignmentId}")
    @PreAuthorize("hasAnyRole('TEACHER', 'STUDENT')")
    public ResponseEntity<AssignmentResponse> getAssignmentById(@PathVariable Long assignmentId) {
        return ResponseEntity.ok(assignmentService.getAssignmentById(assignmentId));
    }
}
